package com.itboye.jmsfhz.main;

import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.adapter.EmploymentAdapter;
import com.itboye.jmsfhz.app.ui.web.WebActivity;
import com.itboye.jmsfhz.app.ui.web.WebEmplomengfaremgnAvtivity;
import com.itboye.jmsfhz.data.model.RecruitBean;
import com.itboye.jmsfhz.data.presenter.RedBagPresenter;
import com.itboye.jmsfhz.widget.EditTextWithDelete;
import com.itboye.jmsfhz.widget.XListView;
import com.itboye.jmsfhz.widget.XListView.IXListViewListener;

/**
 * 就业
 * 
 * @author liutiantian
 * 
 */
public class EmploymentFragment extends BaseFragment implements Observer,
		IXListViewListener, TextWatcher {
	private XListView listviewFollow;
	private RecruitBean Bean;
	private RedBagPresenter presenter;
	private EmploymentAdapter adapter;
	private int page_num = 1;
	private int page_size = 10;
	private String uid;
	String id;
	EditText editextproject;
	ListView listview;

	Handler handler;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_follow;
	}

	@Override
	public void onMyActivityCreated() {
		// 注册广播
		handler = new Handler();
		IntentFilter intentFilterAvart2 = new IntentFilter("changeavart");
		getActivity().registerReceiver(avartBroadcastReceiver,
				intentFilterAvart2);

		IntentFilter intentFilterAvart3 = new IntentFilter("project");
		getActivity().registerReceiver(start, intentFilterAvart3);
		uid = SPUtils.get(getActivity(), null, SpPublicParams.UID, "") + "";
		onIsEmploymengetInfo();

		if (uid == null || uid == "") {
			editextproject.setVisibility(View.GONE);
			listviewFollow.setVisibility(View.GONE);
			listview.setVisibility(View.GONE);
		} else {
			try {

				editextproject.setVisibility(View.VISIBLE);
				onIsEmploymengetInfo();
				listviewFollow.setVisibility(View.VISIBLE);
				listview.setVisibility(View.VISIBLE);
			} catch (Exception e) {
				// TODO: handle exception
			}

		}

		if (page_size <= 5) {
			/** 刷新列表 ***/
			listviewFollow.setXListViewListener(this);
			listviewFollow.setPullLoadEnable(true);
			listviewFollow.setPullRefreshEnable(true);

		} else if (page_size > 9) {
			/** 刷新列表 ***/
			listviewFollow.setXListViewListener(this);
			/** 刷新后获取数据 **/
			listviewFollow.setPullLoadEnable(true);
			/** 刷新 **/
			listviewFollow.setPullRefreshEnable(true);
		}
		editextproject.addTextChangedListener(this);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(getActivity(),
						WebEmplomengfaremgnAvtivity.class);
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Company/detail?id="
								+ Bean.getResult().get(position).getId());

				intent.putExtra("title", "招聘详情");
				startActivity(intent);
			}
		});
		//
		listviewFollow.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(getActivity(),
						WebEmplomengfaremgnAvtivity.class);
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Company/detail?id="
								+ Bean.getResult().get(position - 1).getId());
				intent.putExtra("id", Bean.getResult().get(position - 1)
						.getId());
				intent.putExtra("title", "招聘详情");
				startActivity(intent);
			}
		});

	}

	public void onIsEmploymengetInfo() {
		presenter = new RedBagPresenter(this);
		Bean = new RecruitBean();
		Bean.setPage_num("1");
		Bean.setPage_size(page_size + "");
		uid = SPUtils.get(getActivity(), null, SpPublicParams.UID, "") + "";
		presenter.getInfo(Bean);
		adapter = new EmploymentAdapter(getActivity(), Bean);
	}

	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			editextproject.setVisibility(View.VISIBLE);
			listviewFollow.setVisibility(View.VISIBLE);
			listview.setVisibility(View.VISIBLE);
			//
		};
	};
	BroadcastReceiver start = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			listviewFollow.setVisibility(View.GONE);
			listview.setVisibility(View.GONE);
			editextproject.setVisibility(View.GONE);

		};
	};

	@SuppressWarnings("static-access")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity.getEventType() == presenter.jiuye) {
			Bean = (RecruitBean) entity.getData();
			System.out
					.println("<<<<<<<<<<<<<NM<NKJK" + Bean.getResult().size());
			adapter = new EmploymentAdapter(getActivity(), Bean);

			listviewFollow.setAdapter(adapter);

		} else if (entity.getEventType() == presenter.shousuo) {
			Bean = (RecruitBean) entity.getData();
			adapter = new EmploymentAdapter(getActivity(), Bean);
			listview.setAdapter(adapter);

		}
		try {
			/**
			 * 当数据加载出来后dialog则消失
			 */
			closeProgressDialog();
		} catch (Exception e) {

		}

		stopLoad();

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page_num = 1;
		// page_size = 10;
		Bean.setPage_num(page_num + "");
		// Bean.setPage_size(page_size+"");
		presenter.getInfo(Bean);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page_num++;
		Bean.setPage_num(page_num + "");
		// Bean.setPage_size(page_size+"");
		presenter.getInfo(Bean);
	}

	public void stopLoad() {
		listviewFollow.stopLoadMore();
		listviewFollow.stopRefresh();
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if (editextproject.getText().toString().length() > 0) {
			try {
				if (uid == null || uid == "") {
					listviewFollow.setVisibility(View.GONE);
					listview.setVisibility(View.GONE);
				} else {
					listviewFollow.setVisibility(View.GONE);
					listview.setVisibility(View.VISIBLE);
					presenter.getkeywords(page_num + "", page_size + "",
							editextproject.getText().toString() + "");
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		} else {
			listviewFollow.setVisibility(View.VISIBLE);
			listview.setVisibility(View.GONE);
			Bean.setPage_num(page_num + "");
			Bean.setPage_size(page_size + "");
			presenter.getInfo(Bean);
		}
	}

}
